<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if='detail'>
      <!-- 面包屑 -->
      <div class="break-crumb">
        <span>首页 > </span>
        <span>{{detail.categories[1].name}} ></span>
        <span>{{detail.categories[0].name}} ></span>
        <span>{{detail.name}} </span>
      </div>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧的放大镜 -->
        <div class="media">
          <!-- 图片预览 -->
          <GoodsImage :images='detail.mainPictures' />
          <!-- 销售信息 -->
          <GoodsSales />
        </div>
        <!-- 右侧的商品信息 -->
        <div class="spec">
          <GoodsName :detail='detail' />
          <!-- 商品规格 skuId='1369155864430120962'  -->
          <GoodsSku @sku-info='getSkuInfo' :specs='detail.specs' :skus='detail.skus' />
          <!-- 商品数量控制 -->
          <XtxNumbox v-model="count" :max='detail.inventory'>数量</XtxNumbox>
          <XtxButton type="primary" style="margin-top:20px;" @click="addCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"></div>
          <!-- 注意事项 -->
          <div class="goods-warn"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant'
import { findGoods } from '@/api/product.js'
import { getCurrentInstance, ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsSku from './components/goods-sku'
import { useStore } from 'vuex'

// 组合API的拆分
const useGoods = () => {
  const route = useRoute()
  const detail = ref(null)

  // 获取商品详情数据(动态路由默认是有缓存的)
  findGoods(route.params.id).then(ret => {
    detail.value = ret.result
  })

  return detail
}

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku },
  setup () {
    const instance = getCurrentInstance()
    const store = useStore()
    const detail = useGoods()
    // 当前选中的sku信息
    const skuInfo = ref(null)
    // 获取sku信息
    const getSkuInfo = (info) => {
      console.log(info)
      if (info) {
        detail.value.oldPrice = info.oldPrice
        detail.value.price = info.price
        detail.value.inventory = info.inventory
      }
      skuInfo.value = info
    }

    // 购买商品的数量
    const count = ref(1)

    // 添加购物车
    const addCart = () => {
      // 1. 所有的规格必须选中
      if (!skuInfo.value) {
        // 没有选完整规格
        return instance.proxy.$message({
          type: 'warn',
          text: '请选择所有的规格'
        })
      }
      // 2. 库存必须够
      if (count.value > detail.inventory) {
        return instance.proxy.$message({
          type: 'warn',
          text: '没有更多库存了'
        })
      }

      // 3. 准备加入购物车的信息 id skuId name picture price nowPrice count attrsText selected stock isEffective
      const goods = {
        // 商品id
        id: detail.value.id,
        // 规格Id
        skuId: skuInfo.value.skuId,
        // 商品名称
        name: detail.value.name,
        // 商品的图片
        picture: detail.value.mainPictures[0],
        // 原来的价格
        price: skuInfo.value.oldPrice,
        // 最新价格
        nowPrice: skuInfo.value.price,
        // 商品数量
        count: count.value,
        // 规格详情
        attrsText: skuInfo.value.specText,
        // 是否选中
        selected: true,
        // 库存
        stock: detail.value.inventory,
        // 是否有效
        isEffective: true
      }
      // 4. 加入购物车
      store.dispatch('cart/insertCart', goods)
      instance.proxy.$message({
        type: 'success',
        text: '加入购物车成功'
      })
    }
    return { detail, getSkuInfo, count, addCart }
  }
}
</script>

<style scoped lang='less'>
.break-crumb {
  padding: 25px 0;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
